$(function () {
    formObj.init();
});

var formObj = new function () {
    'use strict';
    return {
        form: null,
        params: {
            iId:0 ,
            status:0,
        },
        init: function () {
            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired($('#bt_modal_confirm,parent.document'));
            this.iId = xhTool.getQuery('id');
            this.status = xhTool.getQuery('status');
            this.listener();
        },
        listener: function () {
            var $this = this;
            this.funObj().uploadFile('finance_img');
            // 提交申请单
            window.top.xhTop.getModalConfirmDom().click(function () {
                $this.funObj().saveApply();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                // //选中图片自动上传
                // rebindUploadCover : function (id) {
                //     $('.file'+id).remove();
                //     $.ajaxFileUpload({
                //         url: baseUrl + 'school_apply/uploadImg',
                //         secureuri: false,
                //         fileElementId: id,                               //file标签的id  
                //         dataType: 'json',                                               //返回数据的类型  
                //         success: function (data) {
                //             if (data.length){
                //                 for (var i=0;i<data.length;i++) {
                //                     var imgUrl = '<input name = "sAttachmentUrl['+id+'][]" class="file'+id+'" type="hidden" value="'+data[i]+'" />';
                //                     $('#'+id).after(imgUrl);
                //                 }
                //             }else {
                //                 $('#'+id).val('');
                //                 window.top.xhTop.showMessage({type: 'error', title: data.error})
                //             }
                //         }
                //     });
                // },
                //保存按钮处理事件
                saveApply:function () {
                    var comment = $('textarea').val();
                    $this.form.submit('school_apply/approvalApply',{id:$this.iId, result: $this.status,comment:comment},function (res) {
                        if(xhExtra.checkError(res)){
                            window.top.xhTop.closeModal();
                            return false;
                        }
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.iId=0;
                        window.top.xhTop.getActiveFrame().obj.applyList.reload();
                        window.top.xhTop.closeModal();
                    },{check:false});
                },
                //验证报错返回s
                initeVal: function (message) {
                    window.top.xhTop.showMessage({type: 'error', title: message});
                    xhExtra.setBtnNormal($('#bt_modal_confirm' , window.top.document));
                },
                uploadFile:function (eleid) {
                    $("#"+eleid).fileinput('destroy').fileinput({
                        language: 'zh', //设置语言
                        uploadUrl: baseUrl + 'school_apply/uploadFile',
                        allowedFileExtensions: ['jpg', 'jpeg', 'png', 'gif', 'bpm'],//接收的文件后缀
                        //
                        allowedPreviewTypes: ['image'],
                        uploadAsync: true, //默认异步上传
                        showUpload: true, //是否显示上传按钮
                        showCaption: false,//是否显示标题
                        dropZoneEnabled: false,
                        browseClass: "btn btn-primary", //按钮样式
                        maxFileSize: 50000,
                        maxFileCount: 10, //表示允许同时上传的最大文件个数
                        enctype: 'multipart/form-data',
                        validateInitialCount: true,
                        height: 200,
                        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
                    });

                    $("#"+eleid).on("fileuploaded", function (event, data, previewId, index) {
                        var url = data.response.msg.url;
                        var fileUrl = '<input name = "sAttachmentUrl['+eleid+'][]" class="file'+eleid+'" type="hidden" value="'+url+'" />';

                        $('#'+eleid).before(fileUrl);
                    });

                    $("#"+eleid).on("filesuccessremove", function (event, id) {
                        var num = id.lastIndexOf('-');
                        var str = id.slice(num + 1);
                        $('input[name="sAttachmentUrl['+eleid+'][]"').eq(str).remove();
                    });
                    $("#"+eleid).on("filedeleted", function (event, key, jqXHR, data) {
                        $('input[name="sAttachmentUrl['+eleid+'][]"').eq(key-1).remove();
                    });

                }
            }
        }
    }
};